from django.db import models

# Create your models here.

class User(models.Model):
    username = models.CharField('用户名', max_length=32, unique=True)
    password = models.CharField('密码', max_length=256)
    phone = models.CharField('手机号码', max_length=15)
    email = models.EmailField('邮箱', max_length=32, unique=True)
    last_login = models.DateTimeField('最后登录时间', null=True, blank=True)
    ## 创建一条数据时，会自动加上时间，修改数据时，时间不变
    add_date = models.DateTimeField('创建日期', auto_now_add=True)
    ## 创建一条数据时，会自动加上时间，修改数据时，时间跟着改变
    mod_date = models.DateTimeField('修改时间', auto_now=True)
    groups = models.ManyToManyField(to='Group')

    def __str__(self):
        return self.username

    class Meta:
        db_table = "rbac_user"



class Group(models.Model):
    title = models.CharField('组名', max_length=32, unique=True)
    groupname = models.CharField('用户组', max_length=32, unique=True)
    interfacepaths = models.ManyToManyField(to='InterfacePath')
    menus = models.ManyToManyField(to='FirstMenu')

    def __str__(self):
        return self.title

    class Meta:
        db_table = "rbac_group"


## 接口路径 + 权限
class InterfacePath(models.Model):
    title = models.CharField('接口名称', max_length=32)
    ## name 用于前端按钮权限控制别名，与前端的 v-has 指定结合使用
    name = models.CharField('前端按键权限控制名', max_length=12)
    ## path 用于接口请求时后端的接口校验
    path = models.CharField('接口路径', max_length=64)
    remarks = models.CharField('备注', null=True, max_length=100)
    # interfacepermissions = models.ManyToManyField(to="InterfacePermission")

    def __str__(self):
        return self.title

    class Meta:
        db_table = "rbac_interfacepath"

# ## 接口权限
# class InterfacePermission(models.Model):
#     title = models.CharField('接口名称', max_length=32)
#     action = models.CharField('接口名字', max_length=32)
#
#     def __str__(self):
#         return self.title
#
#     class Meta:
#         db_table = "rbac_interfacepermission"


## 一级菜单
class FirstMenu(models.Model):
    firstmenu = models.CharField('一级菜单标题', max_length=14)

    def __str__(self):
        return self.firstmenu

    class Meta:
        db_table = "rbac_firstmenu"

class SecondMenu(models.Model):
    secondmenu = models.CharField('二级菜单标题', max_length=14)
    routename = models.CharField('二级路由名称', max_length=10)
    parent = models.ForeignKey('FirstMenu', on_delete=models.CASCADE)
    remark = models.CharField('备注', max_length=255, null=True, help_text='备注')

    def __str__(self):
        return self.secondmenu

    class Meta:
        db_table = "rbac_secondmenu"


















